import java.util.Scanner;

public class MineSweeperGame {
    private String[][] userView;  // 用户看到的视图
    private int[][] mineField;    // 实际的地雷场
    private int size;             // 游戏区域大小
    private int mineCount;        // 地雷数量
    private boolean gameOver;     // 游戏是否结束
    private boolean gameWon;      // 游戏是否胜利

    public MineSweeperGame(int size, int mineCount) {
        this.size = size;
        this.mineCount = mineCount;
        this.userView = new String[size][size];
        this.gameOver = false;
        this.gameWon = false;
        initializeUserView();
    }

    // 初始化用户视图，全部显示为"*"
    private void initializeUserView() {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                userView[i][j] = "*";
            }
        }
    }

    // 显示当前游戏界面
    public void displayGame() {
        // 显示X轴坐标
        System.out.print("  ");
        for (int i = 0; i < size; i++) {
            System.out.print(i + " ");
        }
        System.out.println();

        // 显示游戏区域和Y轴坐标
        for (int i = 0; i < size; i++) {
            System.out.print(i + " ");
            for (int j = 0; j < size; j++) {
                System.out.print(userView[i][j] + " ");
            }
            System.out.println();
        }
    }

    // 开始游戏
    public void startGame() {
        // 生成地雷场
        MineFieldGenerator generator = new MineFieldGenerator(size, mineCount);
        mineField = generator.generateMineField();

        Scanner scanner = new Scanner(System.in);

        System.out.println("欢迎来到扫雷游戏！");
        System.out.println("游戏开始！");

        while (!gameOver && !gameWon) {
            displayGame();
            System.out.println("请输入要点击的坐标 (格式: x y): ");

            try {
                int x = scanner.nextInt();
                int y = scanner.nextInt();

                if (x < 0 || x >= size || y < 0 || y >= size) {
                    System.out.println("坐标超出范围，请重新输入！");
                    continue;
                }

                // 处理用户输入
                GameProcessor processor = new GameProcessor();
                userView = processor.processMove(x, y, userView, mineField);

                // 检查游戏状态
                checkGameStatus(x, y);

            } catch (Exception e) {
                System.out.println("输入格式错误，请重新输入！");
                scanner.nextLine(); // 清除错误输入
            }
        }

        scanner.close();

        if (gameOver) {
            revealAllMines();
            displayGame();
            System.out.println("BOOM! 游戏失败！");
        } else if (gameWon) {
            revealAllMines();
            displayGame();
            System.out.println("恭喜！游戏胜利！");
        }
    }

    // 检查游戏状态
    private void checkGameStatus(int x, int y) {
        // 检查是否踩到地雷
        if (mineField[x][y] == -1) {
            gameOver = true;
            return;
        }

        // 检查是否所有非地雷格子都被揭开
        boolean allNonMineRevealed = true;
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                // 如果是非地雷格子且仍然显示为"*"，说明还没被揭开
                if (mineField[i][j] != -1 && userView[i][j].equals("*")) {
                    allNonMineRevealed = false;
                    break;
                }
            }
            if (!allNonMineRevealed) break;
        }

        if (allNonMineRevealed) {
            gameWon = true;
        }
    }

    // 显示所有地雷
    private void revealAllMines() {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                if (mineField[i][j] == -1) {
                    userView[i][j] = "X"; // 用X表示地雷
                }
            }
        }
    }

    // 主方法
    public static void main(String[] args) {
        // 可以调整游戏大小和地雷数量
        MineSweeperGame game = new MineSweeperGame(8, 10);
        game.startGame();
    }
}